/**
 * @author Javen 
 * @copyright 2018-10-30 15:53:56 javendev@126.com 
 * @description 粒子资源测试
 */


/**
 * 使用resetSystem来控制
 */
cc.Class({
    extends: cc.Component,

    properties: {
        click: {
            default: null,
            type: cc.Node,
        }
    },


    // onLoad () {},

    start() {
        this._initNodeTouchEvent();
        this.clickParticle = this.click.getComponent(cc.ParticleSystem);
    },
    _initNodeTouchEvent() {
        //监听事件
        this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchBegin, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this);
    },

    _destroyTouchEvent() {
        //销毁事件
        this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchBegin, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this);
        cc.log("销毁事件...");
    },

    _onTouchBegin: function (event) {
        //获取当前点击的全局坐标
        let temp = event.getLocation();
        cc.log("点击全局坐标： ", temp.x, temp.y)
        //获取当前点击的局部坐标
        let tempClick = this.click.parent.convertToNodeSpaceAR(temp)
        cc.log("点击全局坐标： ", tempClick.x, tempClick.y)
        this.click.position = tempClick;

        this.click.active = true;

        this.clickParticle.resetSystem();

        this.scheduleOnce(function () {
            this.click.active = false;
        }, 1);
    },

    _onTouchMoved: function (event) {
        cc.log('_onTouchMoved');
    },

    _onTouchEnd: function (event) {
        cc.log('_onTouchEnd');
    },

    _onTouchCancel: function (event) {
        cc.log('_onTouchCancel');
    },

    onDestroy() {
        //销毁事件
        this._destroyTouchEvent();
    },
    toBack() {
        cc.director.loadScene("Scene");
    },

    // update (dt) {},
});